<template>
  <div class="app-container">
    <div>
      <el-button type="primary" class="add-button" size="normal" @click="openAddDialog">添加</el-button>
      <br><br>
    </div>
    <el-table
      v-loading="listLoading"
      :data="page.list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <!--      <el-table-column prop="id" label="变高" />-->
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.$index+1 }}
        </template>
      </el-table-column>
      <el-table-column label="角色名字" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.roleName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="角色编码" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.roleCode }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.remark }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="修改时间">
        <template slot-scope="scope">
          <!--          <i class="el-icon-time" />-->
          <span>{{ scope.row.updateTime }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="created_at" label="创建时间">
        <template slot-scope="scope">
          <!--          <i class="el-icon-time" />-->
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            @click="handleEdit(scope.row.id)"
          >
            编辑</el-button>
          <el-button
            type="danger"
            size="mini"
            @click="handleDelete(scope.row.id)"
          >
            删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <br><br>
    <!-- 分页 -->
    <el-pagination
      class="pagination"
      :current-page="page.currentPage"
      :page-sizes="[10,20,50,100]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.totalCount"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <!-- 添加弹窗 -->
    <el-dialog title="添加" :visible.sync="addDialog">
      <Add @closeAddDialog="closeAddDialog" @fetchData="fetchData" />
    </el-dialog>
    <el-dialog title="修改" :visible.sync="updateDialog">
      <Update :role="role" @closeUpdateDialog="closeUpdateDialog" @fetchData="fetchData" />
    </el-dialog>
  </div>
</template>

<script>
import { delById, findPage, getById } from '@/api/role'
import Add from './add'
import Update from './update'
// import AboutUpdate from './about-update'
export default {
  // filters: {
  //   statusFilter(status) {
  //     const statusMap = {
  //       published: 'success',
  //       draft: 'gray',
  //       deleted: 'danger'
  //     }
  //     return statusMap[status]
  //   }
  // },
  components: {
    // eslint-disable-next-line vue/no-unused-components
    Add,
    Update
  },
  data() {
    return {
      list: null,
      listLoading: true,
      // 控制添加弹窗
      addDialog: false,
      //  控制修改弹窗
      updateDialog: false,
      role: {},
      page: {
        currentPage: 1, // 当前页
        pageSize: 10, // 每页显示条数
        totalPage: 0, // 总页数
        totalCount: 0, // 总条数
        params: {}, // 查询参数对象
        list: [] // 数据
      }
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      findPage(this.page).then(response => {
        this.page = response.page
        // console.log(response.data)
        this.listLoading = false
      })
    },
    handleEdit(id) {
      // 修改
      getById(id).then(res => {
        // eslint-disable-next-line no-empty
        if (res.code !== 20000) {
          this.$message.success(res.error)
        }
        this.role = res.data
        this.updateDialog = true
      })
    },
    closeUpdateDialog() {
      // 关闭修改弹窗
      this.updateDialog = false
    },
    openAddDialog() {
      // 控制添加弹窗打开
      this.addDialog = true
    },
    closeAddDialog() {
      // 控制添加弹窗关闭
      this.addDialog = false
    },
    handleDelete(id) {
      // 删除
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'error'
      }).then(() => {
        delById(id).then(res => {
          this.$message.success(res.message)
          this.fetchData()
        })
      })
    },
    //  分页切换
    handleSizeChange(val) {
      this.page.pageSize = val
      this.fetchData()
    },
    handleCurrentChange(val) {
      this.page.currentPage = val
      this.fetchData()
    }
  }
}
</script>
